package org.example.system.controller;

import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.example.system.entity.Account;
import org.example.system.service.AccountService;
import org.example.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 角色控制层
 */
@RestController
@RequestMapping("/app/account")
@Slf4j
public class AccountController {
    @Autowired
    private AccountService accountService;

    /**
     * 添加角色
     * @param account
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody Account account){
        log.info("--------- AccountController | add | input : {}", JSON.toJSON(account));
        int row = accountService.add(account);
        if(row <= 0){
            return  R.fail("添加失败");
        }
        return R.ok("添加成功");
    }

    /**
     * 修改
     * @param account
     * @return
     */
    @PutMapping("/update")
    public R update(@RequestBody Account account){
        log.info("--------- AccountController | update | input : {}", JSON.toJSON(account));
        if(null == account.getId() || account.getId() <= 0){
            return R.fail("参数错误:ID未传入");
        }
        int row = accountService.update(account);
        if(row <= 0){
            return  R.fail("修改失败");
        }
        return R.ok("修改成功");
    }
    /**
     * 根据userId查询角色数据
     * @param userId
     * @return
     */
    @GetMapping("/{userId}")
    public R getInfo(@PathVariable("userId") Long userId){
        log.info("--------- AccountController | getInfo | input : {}", JSON.toJSON(userId));
        return R.ok(accountService.getOne(userId));
    }
}
